import { createRouter, createWebHashHistory } from 'vue-router'
import HOME from '@/views/home/home'
import storage from 'store'
import { ACCESS_TOKEN } from "../utils/index"

const routes = [
    {
        path:'/',
        name: 'Home',
        component: HOME,
        meta:{
            title:'首页'
        },
        children: [
            {
                path:'/',
                name: 'Index',
                meta:{
                    title:'首页',
                    keepAlive: false
                },
                component: ()=> import("../views/home/index.vue")

            },
            {
                path:'/car',
                name: 'ShopCar',
                meta:{
                    title:'购物车'
                },
                component: ()=> import("../views/home/shopCar.vue")

            },
            {
                path:'/my',
                name: 'My',
                meta:{
                    title:'我的',
                    keepAlive: false
                },
                component: ()=> import("../views/home/my.vue")

            }
        ]
    },
    {
        path:'/bill',
        name: 'Bill',
        component: ()=> import("../views/bill.vue"),
        meta:{
            title:'账户明细',
            keepAlive: true
        }
    },
    {
        path:'/transfer',
        name: 'transfer',
        component: ()=> import("../views/transfer.vue"),
        meta:{
            title:'划转',
            keepAlive: true
        }
    },{
        path:'/transfer-list',
        name: 'transferList',
        component: ()=> import("../views/transfer-list.vue"),
        meta:{
            title:'划转记录',
            keepAlive: true
        }
    },
    {
        path:'/withdraw',
        name: 'Withdraw',
        component: ()=> import("../views/withdraw.vue"),
        meta:{
            title:'提现记录',
            keepAlive: false
        }
    },
    {
        path:'/addAccount',
        name: 'AddAccount',
        component: ()=> import("../views/addAccount.vue"),
        meta:{
            title:'我的收款账户',
            keepAlive: false
        }
    },
    {
        path:'/addReal',
        name: 'addReal',
        component: ()=> import("../views/addReal.vue"),
        meta:{
            title:'实名认证',
            keepAlive: false
        }
    },
    {
        path:'/myAccount',
        name: 'MyAccount',
        component: ()=> import("../views/myAccount.vue"),
        meta:{
            title:'我的收款账户',
            keepAlive: true
        }
    },
    {
        path:'/recharge/create',
        name: 'RechargePage',
        component: ()=> import("../views/recharge.vue"),
        meta:{
            title:'充值',
            keepAlive: false
        }
    },
    {
        path:'/myReward',
        name: 'MyRewardList',
        component: ()=> import("../views/myReward.vue"),
        meta:{
            title:'我的奖励',
            keepAlive: false
        }
    },
    {
        path:'/withdrawal',
        name: 'Withdrawal',
        component: ()=> import("../views/withdrawal.vue"),
        meta:{
            title:'提现',
            keepAlive: true
        }
    },
    {
        path:'/recharge-list',
        name: 'RechargeList',
        component: ()=> import("../views/recharge-list.vue"),
        meta:{
            title:'充值记录',
            keepAlive: true
        }
    },
    {
        path:'/notice',
        name: 'NoticeDetails',
        component: ()=> import("../views/notice-details.vue"),
        meta:{
            title:'公告',
            keepAlive: false
        }
    },
    {
        path:'/noticeList',
        name: 'NoticeList',
        component: ()=> import("../views/notice-list.vue"),
        meta:{
            title:'公告列表',
            keepAlive: false
        }
    },
    {
        path:'/team/list',
        name: 'TeamList',
        component: ()=> import("../views/team-list.vue"),
        meta:{
            title:'团队详情',
            keepAlive: false
        }
    },
    {
        path:'/order/list',
        name: 'OrderList',
        component: ()=> import("../views/order-list.vue"),
        meta:{
            title:'我的订单',
            keepAlive: false
        }
    },
    {
        path:'/mallOrderDetail',
        name: 'MallOrderDetail',
        component: ()=> import("../views/mallOrderDetail.vue"),
        meta:{
            title:'确认订单',
            keepAlive: false
        }
    },
    {
        path:'/login',
        name: 'Login',
        component: ()=> import("../views/login.vue"),
        meta:{
            title:'登录',
            keepAlive: false
        }
    },
    {
        path:'/register',
        name: 'Register',
        component: ()=> import("../views/register.vue"),
        meta:{
            title:'注册',
            keepAlive: false
        }
    },
    {
        path:'/invitation',
        name: 'Invitation',
        component: ()=> import("../views/Invitation.vue"),
        meta:{
            title:'邀请',
            keepAlive: false
        }
    },

]

const routers = createRouter({
    history: createWebHashHistory(),
    routes
})

const whiteList = ['Login','Register']

routers.beforeEach((to,form,next)=>{
    const token = storage.get(ACCESS_TOKEN)
    if(to.meta.title){
        document.title = to.meta.title
    }
    if(!token && !whiteList.includes(to.name)){
        next()
    }else{
        next()
    }
    next()
})
export default routers
